package com.example.cigarettepricesystem;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import java.io.ByteArrayOutputStream;

public class AddCigActivity extends AppCompatActivity implements View.OnClickListener {
    private ImageView ivShowImg;
    private Button btnReadImg;
    private EditText etCName;
    private EditText etCPrice;
    private RadioGroup rgShell;
    private Button btnSubmit;
    private Context context = AddCigActivity.this;
    private SQLiteDatabase db;
    private Bundle bundle;
    private Bitmap bm;
    private byte[] barray;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_cig);
        init();
    }

    private void init() {
        ivShowImg = (ImageView) findViewById(R.id.iv_showImg);
        btnReadImg = (Button) findViewById(R.id.btn_readImg);
        etCName = (EditText) findViewById(R.id.et_cName);
        etCPrice = (EditText) findViewById(R.id.et_cPrice);
        rgShell = (RadioGroup) findViewById(R.id.rg_shell);
        btnSubmit = (Button) findViewById(R.id.btn_submit);
        btnSubmit.setOnClickListener(this);
        btnReadImg.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_submit:
                String cShell = null,cName,cPrice;
                cName = etCName.getText().toString().trim();
                cPrice = etCPrice.getText().toString().trim();
                db = SQLiteDatabase.openOrCreateDatabase(DBManager.DB_PATH + "/" + DBManager.DB_NAME, null);//获取可读写数据库对象
                Cursor cursor = db.query("tb_cigarette",null,
                        "cigaretteName=? AND cigarettePrice=?",
                        new String[]{cName,cPrice},
                        null,null,null);//查询数据库内有无此香烟
                if (cursor.getCount() != 0){
                    Toast.makeText(context, "已存在该香烟，请重新输入！", Toast.LENGTH_SHORT).show();
                }else if (bundle == null){
                    Toast.makeText(context, "请拍摄照片！", Toast.LENGTH_SHORT).show();
                }else if (cName.isEmpty()){
                    Toast.makeText(context, "请输入香烟名称！", Toast.LENGTH_SHORT).show();
                }else if (cPrice.isEmpty()){
                    Toast.makeText(context, "请输入香烟价格！", Toast.LENGTH_SHORT).show();
                } else {
                    ContentValues values = new ContentValues();
                    //获取硬壳|软壳
                    for (int i = 0; i < rgShell.getChildCount(); i++) {
                        RadioButton rd = (RadioButton) rgShell.getChildAt(i);
                        if (rd.isChecked()) {
                            cShell = rd.getText().toString();
                            break;
                        }
                    }
                    //将bitmap转化为byte[]类型
                    ByteArrayOutputStream out = new ByteArrayOutputStream();
                    bm.compress(Bitmap.CompressFormat.PNG, 100, out);
                    barray = out.toByteArray();
                    values.put("cigaretteName",cName);
                    values.put("cigarettePrice",cPrice);
                    values.put("cigaretteShell",cShell);
                    values.put("cigaretteImg",barray);
                    db.insert("tb_cigarette",null,values);
                    Toast.makeText(context, "添加成功！", Toast.LENGTH_SHORT).show();
                    db.close();
                    cursor.close();
                    finish();
                }
                break;
            case R.id.btn_readImg:
                //跳转到拍照页面
                Intent it = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                startActivityForResult(it, Activity.DEFAULT_KEYS_DIALER);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        //回传拍照页面获得的图片数据，赋值给bitmap，并将其设置到imageview中
        if (resultCode == RESULT_OK) {
            bundle = data.getExtras();
            bm = (Bitmap)bundle.get("data");
            ivShowImg.setImageBitmap(bm);
        }
    }
}